/* 
 * File:   systask.h
 * Author: apleilx
 */

#ifndef _USER_MANAGE_H_
#define	_USER_MANAGE_H_

#ifdef   _USER_MANAGE_MODULE_
#define  MY_EXT
#else
#define  MY_EXT  extern
#endif

#include "stdint.h"

#ifdef	__cplusplus
extern "C" {
#endif

#define USER_MAX                     50

#define USER_NAME_LEN                10
#define USER_PASSWORD_LEN            12
#define USER_TEL_LEN                 4
#define USER_BODY_INFO_LEN        	 4
#define USER_IC_CARD_LEN             4
#define USER_OPT_CODE_LEN       	 8

typedef enum
{
    USER_LOGIN_NONE      =        0,   //not login - standerby
    USER_LOGIN_NOR       =        1,   //login from none
    USER_LOGIN_SET       =        2,   //password recheck from nor
} user_login_st_type;

//以下为管理系统权限
#define USRE_RIGHT_ADMIN           (1UL << 30)

#define USER_RIGHT_MANAGEER_MSK     0xC0000000UL
#define USER_RIGHT_OPERATOR_MSK     0x00FFFFFFUL
#define USER_RIGHT_NONE             0x00000000UL

#define USER_RIGHT_RS             (1UL << 0)
#define USER_RIGHT_EDIT           (1UL << 1)
#define USER_RIGHT_PAR_NOR        (1UL << 2)
#define USER_RIGHT_PAR_SYS        (1UL << 3)
#define USER_RIGHT_PAR_FAC        (1UL << 4)

typedef enum
{
    USER_OK = 0,
    USER_ERR_LOGIN,
    USER_ERR_ID,
    USER_ERR_FINGER,
    USER_ERR_REFINGER,
    USER_ERR_IC_CARD,
    USER_ERR_REIC_CARD,
    USER_ERR_NAME,
    USER_ERR_RENAME,
    USER_ERR_RIGHT,
    USER_ERR_SLAVE,
    USER_ERR_PASSWORD,
    USER_ERR_OPERATOR,
    USER_ERR_TEL,
    USER_ERR_NO_SPACE,
} user_err_type;

typedef struct 
{
	uint32_t right;         //每位表示一种权限 //最高8bit为管理系统权限，低24bit为用户自定义权限
    uint8_t  b_enabled;     //是否已开启
    uint8_t  id_owner; 		//上一级user
    uint32_t creat_time;	//创建时间
    uint32_t lock_time;		//锁定时间
    uint8_t  name[USER_NAME_LEN];  //全局唯一
    uint8_t  password[USER_PASSWORD_LEN]; // string
    uint8_t  opt_code[USER_OPT_CODE_LEN];	// string
    uint8_t  tel[USER_TEL_LEN];	// bin
    uint8_t  body_info[USER_BODY_INFO_LEN]; //bin 全局唯一
    uint8_t  ic_card[USER_IC_CARD_LEN]; //bin 全局唯一
} user_type;

//外部可读取任意数据，但禁止直接修改。必须通过接口函数设定参数
typedef struct 
{
    uint8_t id_now; //当前选择的ID
    uint8_t st_login; //当前USER登陆状态
    uint8_t supper_password[USER_PASSWORD_LEN];
    uint32_t user_right_mask; //last 24bits
    user_type user_now; //当前USER
    user_type user_list[USER_MAX+1]; //USER list //user0 is supper right
    uint8_t enable_list[USER_MAX+4]; //获取已开启的用户列表，按顺序存储id
    uint8_t slave_list[USER_MAX+4]; //user_slave_list_get 获取从属用户列表，按顺序存储id
    uint8_t rev_end[4];
} user_mange_type;

MY_EXT user_mange_type user ;

/*init*/
void user_manage_init(void);
user_err_type user_login_st_set(uint8_t St);
user_err_type user_right_mask_set(uint32_t msk);

/*add and delete*/
user_err_type user_add(uint32_t right, uint8_t* Name, uint8_t * password, uint8_t * tel, uint8_t * finger, uint8_t * icard); //tel,finger,icard可以为0，表示不用。
user_err_type user_add_force(uint8_t id, uint32_t right, uint8_t* Name, uint8_t * password); //仅0号id登陆后可使用。如果id已经开启，覆盖掉。操作对象不可是0号ID
user_err_type user_id_delete(uint8_t id);
user_err_type user_name_delete(uint8_t* name);

/*user slect*/
user_err_type user_id_sel(uint8_t id);
user_err_type user_name_sel(uint8_t *name);

/*right check*/
user_err_type user_slave_check(uint8_t id_master, uint8_t id_slave);

/*user argv set. --user must be login and have right to edit*/
user_err_type user_right_set(uint8_t id, uint32_t right);
user_err_type user_right_add(uint8_t id, uint32_t right);
user_err_type user_right_del(uint8_t id, uint32_t right);
user_err_type user_right_check(uint8_t id, uint32_t right);
user_err_type user_and_slave_right_add(uint8_t id, uint32_t right);
user_err_type user_and_slave_right_del(uint8_t id, uint32_t right);
user_err_type user_password_set(uint8_t id, uint8_t *password);
user_err_type user_operator_set(uint8_t id, uint8_t *opt_code);
user_err_type user_name_set(uint8_t id, uint8_t *name);
user_err_type user_finger_set(uint8_t id, uint8_t *finger);
user_err_type user_icard_set(uint8_t id, uint8_t *icard);
user_err_type user_tel_set(uint8_t id, uint8_t *tel);
user_err_type user_creat_time_set(uint8_t id, uint32_t time);

/*get id*/
uint8_t user_get_id_with_name(uint8_t *name);  // 0xff表示无ID
uint8_t user_get_id_with_finger(uint8_t *finger); // 0xff表示无ID
uint8_t user_get_id_with_icard(uint8_t *icard); // 0xff表示无ID

/*check*/
user_err_type user_password_check(uint8_t id, uint8_t *password);
user_err_type user_operator_check(uint8_t id, uint8_t *opt_code);
user_err_type user_finger_check(uint8_t id, uint8_t *finger);
user_err_type user_icard_check(uint8_t id, uint8_t *icard);
uint8_t is_user_password_empty(uint8_t id);

/*compare*/
user_err_type user_password_compare(uint8_t *pw1, uint8_t*pw2);
user_err_type user_operator_compare(uint8_t *opt1, uint8_t*opt2);
user_err_type user_finger_compare(uint8_t *fg1, uint8_t*fg2);
user_err_type user_icard_compare(uint8_t *icd1, uint8_t*icd2);

/*amount*/
uint8_t user_amount(void);
uint8_t user_enable_list_get(void);
uint8_t user_slave_list_get(uint8_t master_id); // 包含从机及从机的从机
uint8_t user_des_slave_list_get(uint8_t master_id); // 仅直属从机

#ifdef	__cplusplus
}
#endif

#undef MY_EXT

#endif	/* _USER_MANAGE_H_ */

